﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Domain.Core.Commons
{
    /// <summary>
    /// 位域运算辅助类，重写了几个运算符
    /// </summary>
    public class FlagsOperator
    {
        int _value;
        public FlagsOperator(ValueType value)
        {
            _value = (int)value;
        }

        /// <summary>
        ///  位域加法
        /// </summary>
        /// <param name="d1"></param>
        /// <param name="d2"></param>
        /// <returns></returns>
        public static int operator +(FlagsOperator d1, FlagsOperator d2)
        {
            return d1._value.GetHashCode() | d2._value.GetHashCode();
        }
        /// <summary>
        /// 位域减法
        /// </summary>
        /// <param name="d1"></param>
        /// <param name="d2"></param>
        /// <returns></returns>
        public static int operator -(FlagsOperator d1, FlagsOperator d2)
        {
            return d1._value.GetHashCode() & (~d2._value.GetHashCode());
        }
        /// <summary>
        /// 包含
        /// </summary>
        /// <param name="d1"></param>
        /// <param name="d2"></param>
        /// <returns></returns>
        public static bool operator ==(FlagsOperator d1, FlagsOperator d2)
        {
            return (d1._value.GetHashCode() & d2._value.GetHashCode()) > 0;
        }
        /// <summary>
        /// 不包含
        /// </summary>
        /// <param name="d1"></param>
        /// <param name="d2"></param>
        /// <returns></returns>
        public static bool operator !=(FlagsOperator d1, FlagsOperator d2)
        {
            return (d1._value.GetHashCode() & d2._value.GetHashCode()) == 0;
        }

    }

}
